<template>
  <div class="profile">
    <!-- 未登录头部状态 -->
    <div class="not-login header" v-if="!user">
      <img @click="$router.push('/login')" src="@/assets/images/mobile.png" alt="" />
      <span>登录 / 注册</span>
    </div>
    <!-- /未登录头部状态 -->

    <!-- 已登录头部状态 -->
    <div class="login-box header" v-else>
      <div class="user-info">
        <div class="left">
          <img :src="userInfo.photo" alt="" />
          <span>{{userInfo.name}}</span>
        </div>
        <van-button to="/user" type="default">编辑资料</van-button>
      </div>
      <div class="user-data">
        <div class="data-item">
          <span>{{userInfo.art_count}}</span>
          <span>头条</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.fans_count}}</span>
          <span>粉丝</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.follow_count}}</span>
          <span>关注</span>
        </div>
        <div class="data-item">
          <span>{{userInfo.like_count}}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- /已登录头部状态 -->

    <!-- 导航 -->
    <van-grid :column-num="2" class="myGrid" >
      <van-grid-item icon="photo-o" text="收藏">
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
      </van-grid-item>
      <van-grid-item icon="photo-o" text="历史">
        <template #icon>
          <i class="toutiao toutiao-lishi"></i>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- 其他导航 -->
    <div class="out-box">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <van-cell @click="onLogout" v-if="user" title="退出" class="logout-btn"/>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUsrSelfInfo } from '@/api/user.js'
export default {
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {

    ...mapState(['user'])
  },

  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },

  methods: {
    async  loadUserInfo () {
      try {
        const res = await getUsrSelfInfo()
        console.log(res)
        this.userInfo = res
      } catch (error) {
        Toast('获取用户信息失败')
      }
    },
    onLogout () {
      Dialog.confirm({
        title: '黑马头条',
        message: '确认退出该账号吗？'
      })
        .then(() => {
          this.$store.commit('removeUser')
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped lang="less">
.profile {
  .header {
    width: 100%;
    height: 401px;
    background-image: url(~@/assets/images/banner.png);
    background-size: 100% 100%;
  }
  .not-login {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img {
      width: 132px;
      height: 132px;
    }
    span {
      font-size: 28px;
      margin-top: 10px;
      color: #fff;
    }
  }
  .login-box {
    overflow: hidden;
    .user-info {
      width: 100%;
      height: 132px;
      margin-top: 116px;
      box-sizing: border-box;
      padding: 0 32px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      .left {
        display: flex;
        align-items: center;
        img {
          overflow: hidden;
          width: 132px;
          height: 132px;
          border-radius: 50%;
          border: 1px solid #fff;
        }
        span {
          font-size: 28px;
          color: #fff;
          margin-left: 22px;
        }
      }
      .van-button {
        width: 115px;
        height: 32px;
        background: #ffffff;
        border-radius: 16px;
        font-size: 20px;
        color: #666;
        padding: 0;
      }
    }
    .user-data {
      width: 100%;
      height: 156px;
      // background-color: red;
      display: flex;
      .data-item {
        flex: 1;
        display: flex;
        align-items: center;
        flex-direction: column;
        justify-content: center;
        color: #fff;
      }
      span:nth-child(1) {
        font-size: 26px;
        margin-bottom: 8px;
      }
      span:nth-child(2) {
        font-size: 22px;
      }
    }
  }
  .myGrid {
    .toutiao {
      font-size: 45px;
    }

    .toutiao-shoucang {
      color: #eb5253;
    }

    .toutiao-lishi {
      color: #ff9d1d;
    }
  }
  .out-box{
    margin: 10px 0;
  }
  .logout-btn{
    text-align: center;
    color:red
  }
}
</style>
